<?php

namespace tools;

class Common
{
    function curl_request($url, $method = 'get', $data = null, $https = true)
    {
        $ch = curl_init($url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        if ($https === true) {
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
            curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        }
        if ($method === 'post') {
            curl_setopt($ch, CURLOPT_POST, true);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        }
        $result = curl_exec($ch);
        curl_close($ch);
        return $result;
    }

    function xmlToArray($xml, $isfile = false)
    {
        //禁止引用外部xml实体
        libxml_disable_entity_loader(true);
        if ($isfile) {
            if (!file_exists($xml)) return false;
            $xmlstr = file_get_contents($xml);
        } else {
            $xmlstr = $xml;
        }
        $result = json_decode(json_encode(simplexml_load_string($xmlstr, 'SimpleXMLElement', LIBXML_NOCDATA)), true);
        return $result;
    }

    function deal_search($url, $name)
    {
        $z_url = $url . '/?wd=' . $name;
        $z_text = $this->curl_request($z_url);
        preg_match_all("/<id>(.*)<\/id>/U", $z_text, $data_id);
        $id = implode(",", preg_replace("/<\/?[^>]+>/", "", $data_id[0]));
        $data = "";
        if (!empty($id)) {
            $q_url = $url . '?ac=videolist&ids=' . $id;
            $data = $this->xmlToArray($this->curl_request($q_url))['list']['video'];
            if (empty($data[0])) {
                $z_data = $data;
                $data = null;
                $data[0] = $z_data;
            }
            for ($i = 0; $i < sizeof($data); $i++) {
                if (empty($data[$i]['des'])) {
                    $data[$i]['des'] = "暂无";
                } else {
                    $data[$i]['des'] = mb_substr(preg_replace("/<\/?[^>]+>/", "", $data[$i]['des']), 0, 100, "utf-8") . "...";
                }
            }

        }
        return $data;
    }

    function deal_detail($url)
    {
        $data = $this->xmlToArray($this->curl_request($url))['list']['video'];
        if (empty($data['des'])) {
            $data['des'] = "暂无";
        } else {
            $data['des'] = preg_replace("/<\/?[^>]+>/", "", $data['des']);
        }
        if (empty($data['lang'])) {
            $data['lang'] = "未知";
        }
        if (empty($data['year'])) {
            $data['year'] = "未知";
        }
        if (empty($data['actor'])) {
            $data['actor'] = "未知";
        }
        if (!is_string($data['dl']['dd'])) {
            if (sizeof(explode("#", $data['dl']['dd'][0])) > 1) {
                $data['url'] = explode("#", $data['dl']['dd'][0]);
            } else {
                $data['url'] = $data['dl']['dd'];
            }
        } else {
            $data['url'] = explode("#", $data['dl']['dd']);
        }
        return $data;
    }

}
	
	